package com.yuerlive.rainbow.module.mine;

import android.content.Intent;
import android.os.Handler;

import com.wuxiaolong.pullloadmorerecyclerview.PullLoadMoreRecyclerView;
import com.yuerlive.rainbow.R;
import com.yuerlive.rainbow.base.BaseActivity;
import com.yuerlive.rainbow.controller.LiveRoomController;
import com.yuerlive.rainbow.entity.MyMsgData;
import com.yuerlive.rainbow.entity.MyMsgInfo;
import com.yuerlive.rainbow.module.homepage.HomePageActivity;
import com.yuerlive.rainbow.module.live.LiveRoomActivity;
import com.yuerlive.rainbow.module.mine.adapter.MyMessageAdapter;
import com.yuerlive.rainbow.module.mine.presenter.MyMessageContract;
import com.yuerlive.rainbow.module.mine.presenter.MyMessagePresenter;
import com.yuerlive.rainbow.module.web.WebViewActivity;
import com.yuerlive.rainbow.utils.L;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by xiaoyi on 2017/5/18.
 */

public class MyMessageActivity extends BaseActivity implements MyMessageContract.View{
    @BindView(R.id.pullLoadMoreRecyclerView)
    PullLoadMoreRecyclerView refreshLayout;

    List<MyMsgInfo> mDatas = new ArrayList<>();
    MyMessageAdapter adapter;
    private MyMessageContract.Presenter presenter;
    private boolean isLoading;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_mymessage;
    }

    @Override
    protected void initViews() {
        setTitle("我的消息");
        presenter = new MyMessagePresenter(this);
        presenter.getMyMessage(false);

        refreshLayout.setColorSchemeResources(R.color.colorPrimary);
        refreshLayout.setLinearLayout();
        refreshLayout.setOnPullLoadMoreListener(new PullLoadMoreRecyclerView.PullLoadMoreListener() {
            @Override
            public void onRefresh() {
                mDatas.clear();
                presenter.getMyMessage(false);
            }

            @Override
            public void onLoadMore() {
                presenter.getMyMessage(true);
            }
        });
        adapter = new MyMessageAdapter(this, mDatas);
        adapter.setOnItemClickListener((v, p) -> {
            Intent i = new Intent();
            MyMsgInfo info = mDatas.get(p);
            switch (info.getTargetType()) {
                case 0: //nothing
                    break;
                case 2:  //live
                    if(info.getState()!=0) {
                        i.putExtra("liveId", info.getLiveId());
                        i.putExtra("chatroomId", info.getChatRoomId());
                        i.putExtra("liveCover", info.getLiveIcon());
                        i.setClass(MyMessageActivity.this, LiveRoomActivity.class);
                        LiveRoomController.getInstance().enterLiveRoom(this,i);
                    }else{
                        i.setClass(mActivity, HomePageActivity.class);
                        i.putExtra("otherId",info.getUserId());
                        jumpToActivity(i);
                    }
                    break;
                case 3:  //web
                    i.putExtra("url", info.getUrl());
                    i.setClass(this, WebViewActivity.class);
                    jumpToActivity(i);
                    break;
            }
        });
        refreshLayout.setAdapter(adapter);
    }

    @Override
    public void showMessageData(List<MyMsgInfo> messageList, int page) {
        if (page == 1) {
            mDatas.clear();
        }
        if (messageList == null) {
            messageList = new ArrayList<>();
        }
        mDatas.addAll(messageList);
        adapter.notifyDataSetChanged();
        refreshLayout.setPullLoadMoreCompleted();
        L.e(TAG, "showMessageData" + adapter.getItemCount() + "::");

    }

    @Override
    public void setPresenter(MyMessageContract.Presenter presenter) {
        super.presenter = presenter;
    }

    @Override
    public void showLoading() {
        refreshLayout.setRefreshing(true);
    }

    @Override
    public void hideLoading() {
        refreshLayout.setPullLoadMoreCompleted();
    }
}
